﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.Laboratory.Algorithm
{
    /// <summary>
    /// http://blog.csdn.net/maotoula/article/details/7032975
    /// 2011年度变态迷宫数学题：从左边入口处的 2011 进去，在迷宫里转悠，最后变成 2012 从右边出来。你可以在迷宫里转圈，可以重复之前走过的路，但不能往回退着走。
    /// </summary>
    class Maze
    {

        public struct Node
        {
            public int x;//记录上一次的位置
            public double y;//记录结果
            public int num;
            public string str;//记录走的方向
        }

        static int getResult(List<Node> vec)
        {
            int i = 0;
            int j = 0;
            while (i < 3000000)
            {
                Node n = vec[i];
                Node temp = n;
                int k = i;
                switch (temp.num)
                {
                    case 7:
                        temp.y = temp.y / 2;
                        temp.y = temp.y + 7;
                        temp.str = "/2+7";
                        temp.x = i;
                        vec.Add(temp);

                        temp = n;
                        temp.y = temp.y - 5;
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y * 3;
                        temp.str = "-5*3";
                        temp.x = i;
                        temp.num = 3;
                        vec.Add(temp);

                        temp = n;
                        temp.x = i;
                        temp.y = temp.y * 3;
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y - 5;
                        temp.str = "*3-5";
                        temp.num = 5;
                        vec.Add(temp);
                        break;
                    case 2:
                        temp.y = temp.y + 7;
                        temp.y = temp.y / 2;
                        temp.str = "+7/2";
                        temp.x = i;
                        vec.Add(temp);

                        temp = n;
                        temp.y = temp.y - 5;//-5 *3
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y * 3;
                        temp.x = i;
                        temp.str = "-5*3";
                        temp.num = 3;
                        vec.Add(temp);

                        temp = n;
                        temp.x = i;
                        temp.y = temp.y * 3;
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y - 5;
                        temp.num = 5;
                        temp.str = "*3-5";
                        vec.Add(temp);
                        break;
                    case 3:
                        temp.y = temp.y - 5;
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y * 3;
                        //temp.num=temp.num;
                        temp.str = "-5*3";
                        temp.x = i;
                        vec.Add(temp);

                        temp = n;
                        temp.y = temp.y + 7;//-5 *3
                        temp.y = temp.y / 2;
                        temp.x = i;
                        temp.str = "+7/2";
                        temp.num = 2;
                        vec.Add(temp);


                        temp = n;
                        temp.x = i;
                        temp.y = temp.y / 2;
                        temp.y = temp.y + 7;
                        temp.str = "/2+7";
                        temp.num = 7;
                        vec.Add(temp);
                        break;
                    case 5:
                        temp.y = temp.y * 3;
                        if (temp.y == 2012)
                            return i;
                        temp.y = temp.y - 5;
                        temp.str = "*3-5";
                        temp.x = i;
                        vec.Add(temp);

                        temp = n;
                        temp.y = temp.y + 7;//-5 *3
                        temp.y = temp.y / 2;
                        temp.str = "+7/2";
                        temp.x = i;
                        temp.num = 2;
                        vec.Add(temp);

                        temp = n;
                        temp.x = i;
                        temp.y = temp.y / 2;
                        temp.y = temp.y + 7;
                        temp.str = "/2+7";
                        temp.num = 7;
                        vec.Add(temp);
                        break;
                }
                i++;
            }
            return -1;
        }

        static int getList(List<Node> vec, int index)
        {
            Node n = vec[index];
            if (n.x == 0 || n.x == 1)
            {
                Console.WriteLine(n.str + "  x:" + n.x + "   y:" + n.y + "    num:" + n.num);
                //cout << n.str << "  " << endl;
                if (n.x == 0)
                    return 0;
                else
                    return 1;
            }
            int k = getList(vec, n.x);
            Console.WriteLine(n.str+"  x:"+n.x+"   y:"+n.y+"    num:"+n.num);
            //cout << n.str << "  " << endl;
            return k;
        }

        public static void main()
        {
            List<Node> vec=new List<Node>();
            Node n=new Node();
            n.x = 0; n.y = 2011 + 7; n.num = 7;//初始时选择+7
            vec.Add(n);
            //n.x = 0; n.y = 2011 / 2; n.num = 2;//或者初始时选择/2
            //vec.Add(n);
            int x = getResult(vec);//广域搜索
            if (x > 0)
            {
                //cout << "找到结果路线:" << endl;
                Console.WriteLine("找到结果路线:");
            }
            else
            {
                Console.WriteLine("找不到路线:");
                //cout << "找不到路线:" << endl;
                //return 1;
                Console.Read();
                return;
            }
            x = getList(vec, x);
            if (x == 0)
                Console.WriteLine("初始选择2011+7 即：2018");
                //cout << "初始选择2011+7 即：2018";
            else
                Console.WriteLine("初始选择2011/2 即：1005");
                //cout << "初始选择2011/2 即：1005";
            //return 0;
            Console.Read();
        }
    }
}
